package com.unipay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unipay.model.system.SysRole;
import com.unipay.model.system.SysUserRole;
import com.unipay.model.vo.AssginRoleVo;
import com.unipay.model.vo.SysRoleQueryVo;
import com.unipay.system.mapper.SysRoleMapper;
import com.unipay.system.mapper.SysUserRoleMapper;
import com.unipay.system.service.SysRoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: unipay-auth-parent
 * @Author: cmy
 * @CreateTime: 2023-02-01  20:48
 * @Description: TODO
 */

//public class SysRoleServiceImpl implements SysRoleService<> {
@Service
@Transactional                  // TODO 先继承、再实现
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

/*
    TODO 原始方式：注入        ---这里，service调mapper已经做了封装---
    @Autowired
    private SysRoleMapper sysRoleMapper;
*/
    // 分页条件查询
    @Override
    public IPage<SysRole> selPage(Page<SysRole> pageParam, SysRoleQueryVo sysRoleQueryVo) {
        IPage<SysRole> pageModel = baseMapper.selPage(pageParam, sysRoleQueryVo);
        return pageModel;
    }


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    // 根据用户获取角色数据
    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //（1）获取所有角色
        List<SysRole> roles = baseMapper.selectList(null);
        //（2）根据用户id查询，对应角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        //获取用户已分配的角色
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(wrapper);
        //获取用户已分配的角色id
        ArrayList<String> userRoleIds = new ArrayList<>();
        for(SysUserRole userRole: sysUserRoles){
            userRoleIds.add(userRole.getRoleId());
        }
        //创建返回的Map
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles",roles);
        returnMap.put("userRoleIds",userRoleIds);
        return returnMap;
    }
    // 根据用户分配角色
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //（1）根据用户id删除原来分配的角色
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", assginRoleVo.getUserId());   // 不唯一
        sysUserRoleMapper.delete(queryWrapper);
        //（2）重新，获取所有的角色id
        List<String> roleIdList = assginRoleVo.getRoleIdList();
        for (String roleId : roleIdList){
            if(roleId != null){
                // 创建新的关联对象
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(assginRoleVo.getUserId());
                userRole.setRoleId(roleId);
                // 保存
                sysUserRoleMapper.insert(userRole);
            }
        }
    }
}
